﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormChangeInfo : Form
    {
        public User user;
        public FormChangeInfo()
        {
            InitializeComponent();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if (CheckName())//判断名称是否已存在
                {
                    UpdateSort();
                }
            }
        }
        //非空验证
        private bool CheckInput()
        {
            bool flag = true;
            if (this.text_NewUserName.Text.Trim().Length == 0)
            {
                MessageBox.Show("名称不能为空！");
                flag = false;
                this.text_NewUserName.Text = "";
            }
            return flag;
        }
        //检查是否已存在
        private bool CheckName()
        {
            bool flag = true;
            DBHelper dBHelper = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("select * from user where username = '{0}'", text_NewUserName.Text.Trim());
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                dBHelper.OpenConnection();
                MySqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    flag = false;
                    MessageBox.Show("名称已存在！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                reader.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("检查失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
            return flag;
        }

        //修改
        private void UpdateSort()
        {
            DBHelper db = new DBHelper();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("Update user set username = '{0}' where username = '{1}'", text_NewUserName.Text.Trim(), user.Username);
                MySqlCommand cmd = new MySqlCommand(sb.ToString(), db.Connection);
                db.OpenConnection();
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("修改成功！");
                    this.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("修改失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                db.CloseConnection();
            }
        }
    }
}
